package pwd.allen.aop.pointcut;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import pwd.allen.aop.MyAop;

import java.lang.reflect.Method;

/**
 * 定义静态切入点，默认不过滤类
 *
 * @author 门那粒沙
 * @create 2020-08-30 14:41
 **/
public class MyStaticPointcut extends StaticMethodMatcherPointcut {

    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        System.out.println("Static check for " + method.getName());
        return method.getName().equals("divide");
    }

    /**
     * 重写类过滤逻辑，只处理MyAop类型
     * StaticMethodMatcherPointcut默认的实现是：总是返回true，即不过滤任何类
     *
     * @return
     */
    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> clazz) {
                System.out.println("Static check for " + clazz.getName());
                return clazz == MyAop.class;
            }
        };
    }
}
